﻿--iCat

local moduleName = "Pet"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Party Pet Bar",

	["Texture"] = true,
	["The texture used for the pet health bar."] = true,

	["Frame Width"] = true,
	["Change pet frame width"] = true,
}
if GetLocale() == "zhCN" then
	L[moduleName] = "宠物生命条"

	L["Texture"] = "贴图"
	L["The texture used for the pet health bar."] = "生命条的贴图"

	L["Frame Width"] = "宽度"
	L["Change pet frame width"] = "设置宠物条的宽度"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "寵物生命條"

	L["Texture"] = "貼圖"
	L["The texture used for the pet health bar."] = "生命條的貼圖"

	L["Frame Width"] = "寬"
	L["Change pet frame width"] = "設置寵物條的寬度"
end

----------------------------------
--      Local Declaration      --
----------------------------------

local SharedMedia     = LibStub("LibSharedMedia-3.0")
local SharedMediaType = SharedMedia.MediaType and SharedMedia.MediaType.STATUSBAR or "statusbar"
local tbl = SharedMedia:List(SharedMediaType)

local UnitHealthMax = UnitHealthMax
local UnitHealth = UnitHealth
local UnitName = UnitName

----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName)

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		texture = {
			type     = 'select',
			name     = L["Texture"],
			desc     = L["The texture used for the pet health bar."],
			get      = "GetSetTexture",
			set      = "GetSetTexture",
			values = tbl
		},
		width = {
			type = "range",
			name = L["Frame Width"],
			desc = L["Change pet frame width"],
			min = 3,
			max = 20,
			step = 1,
			disabled = function() return InCombatLockdown() end,
			get  = "GetSetWidth",
			set  = "GetSetWidth"
		}
	}
}

plugin.defaultDB = {
--	position = 2,
	profile = {
		texture   = "bar21",
--		width = 8
		style = {
			width = 100,
			height = 30,
			yoffest = 4,
			columnspacing = 150,
			padding = 0
		}
	}
}
----------------------------------
--      Module Functions        --
----------------------------------
function plugin:OnInitialize()
		self.core:GetModule("HealthBar").head[moduleName] = true
end

function plugin:OnEnable()
--	self:RegisterEvent("UNIT_PET", "UpdateUnit")
--	self:RegisterEvent("UNIT_HEALTH", "UpdateUnit")
--	self:RegisterEvent("UNIT_MAXHEALTH", "UpdateUnit")
	self.core.frames[moduleName] = {}
	self.db.profile.style.padding =  - self.db.profile.style.width - self.core.db.profile.visual.padding
	self:NewMaster()
end

function plugin:NewMaster()
 	self.core.master[moduleName] = self.core:CreateMaster(moduleName, self.core.master.core, self.db.profile.style, "pet")
	if self.core.master[moduleName] then
		self.core.master[moduleName]:ClearAllPoints()
		self.core.master[moduleName]:SetPoint("TOPLEFT", self.core.master.core, "TOPLEFT", self.db.profile.style.padding, 0)
		self.core.master[moduleName]:Show()
	else
		self.core:GetModule("HealthBar").head[moduleName] = nil
	end
end
--[[
function plugin:Create(frame)
	local texture = SharedMedia:Fetch(SharedMediaType, self.db.profile.texture)
	local bar = frame:CreateTexture(nil, "ARTWORK")
	bar:SetTexture(texture)
	bar:SetTexCoord(1,0, 0,0, 1,1, 0,1)
--	bar:SetWidth(self.db.profile.width)
--	bar:ClearAllPoints()
	bar:SetHeight(28)
	bar:SetPoint("BOTTOMRIGHT", -1, 1)
	bar:SetPoint("BOTTOMLEFT", 1, 1)
	bar:Hide()

	local back = frame:CreateTexture(nil, "BACKGROUND")
	back:SetTexture(texture)
	back:SetTexCoord(1,0, 0,0, 1,1, 0,1)
	back:SetWidth(1)
	back:SetPoint("BOTTOM", 0, 1)
	back:SetPoint("TOP", 0, -1)
	back:Hide()
	bar.back = back

	return bar
end

function plugin:OnUpdate(frame)
	local pet = frame.unit:gsub("^([^%d]*)([%d]*)", "%1pet%2")
	local bar = self.frames[frame.unit]

	if (not UnitExists(pet)) or (not bar) then return end
	bar = bar.bar

	if UnitHealthMax(pet) > 0 then
		local r, g, b, p
		if not UnitIsDead(pet) then
			p = UnitHealth(pet) / UnitHealthMax(pet)
			bar:SetHeight(28 * p)
			if p >= 0.5 then
				r, g, b = (1 - p) * 2, 1, 0
			else
				r, g, b = 1, p * 2, 0
			end
		else
			r, g, b= 0.5, 0.5, 0.5
			bar:SetHeight(28)
		end

		bar:SetVertexColor(r, g, b)
		bar.back:SetVertexColor(r, g, b, 0.8)
--		bar:SetWidth(frame.parts["Name"]:GetWidth()+3)
		bar:Show()
		bar.back:Show()
		self.core:GetSetPadding("left", (self.core.db.profile.visual.padding + self.db.profile.width))
	else
		bar:Hide()
		bar.back:Hide()
		self.core:GetSetPadding("left", self.core.db.profile.visual.padding)
	end
	self.core:UpdateSide(frame)
end
]]
----------------------------------
--      Option Handlers         --
----------------------------------
function plugin:GetSetTexture(_, v)
	if type(v) == "nil" then
		for i, k in pairs(tbl) do
			if k == self.db.profile.texture then
				return i
			end
		end
	end

	if self.db.profile.texture ~= tbl[v] then
		self.db.profile.texture = tbl[v]

		for u, f in pairs(plugin.frames) do
			f.bar:SetTexture(SharedMedia:Fetch(SharedMediaType, self.db.profile.texture))
			f.bar.back:SetTexture(SharedMedia:Fetch(SharedMediaType, self.db.profile.texture))
		end
		
	end
end

function plugin:GetSetWidth(_, v)
	if type(v) == "nil" then return self.db.profile.width end

	if self.db.profile.width ~= v then
		self.db.profile.width = v
		self.master:SetWidth(self.db.profile.width)
		for u, f in pairs(plugin.frames) do
			f:SetWidth(self.db.profile.width)
		end
		self.master:SetPoint("TOPLEFT", self.core.master, "TOPLEFT", - self.db.profile.width - 1, 0)
		self.master:SetAttribute("columnSpacing", self.core.db.profile.visual.columnspacing + self.core.db.profile.visual.width - self.db.profile.width)
		self:UpdateAll()
	end
end

function plugin:GetSetButtons(v)
	if type(v) == "nil" then return self.db.profile.mousebutton end

	if self.db.profile.mousebutton ~= v then
		self.db.profile.mousebutton = v

		self:UpdateAllWith(function(frame, bar)
			frame:SetAttribute("unit3", nil)
			frame:SetAttribute("unit4", nil)
			frame:SetAttribute("unit5", nil)
		end)

		self:UpdateAll()
	end
end
